package com.itheima.app.controller;

import com.itheima.app.interceptor.UserHolder;
import com.itheima.app.manager.UserManager;
import com.itheima.domain.db.User;
import com.itheima.domain.db.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.ws.rs.POST;
import java.io.IOException;
import java.util.Map;

@RestController
public class UserController {

    @Autowired
    private UserManager userManager;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    // 1.保存用户
    @PostMapping("/user/save")
    public ResponseEntity save(@RequestBody User user) {
        return userManager.save(user);
    }

    // 2.根据手机号查询
    @GetMapping("/user/findByPhone")
    public ResponseEntity findByPhone(String phone) {
        return userManager.findByPhone(phone);
    }

    // 发送短信验证码
    @PostMapping("/user/login")
    public ResponseEntity sendSms(@RequestBody Map<String, String> param) {

        if (stringRedisTemplate.hasKey("freeze:login")){

            return ResponseEntity.status(500).body("冻结登陆");

        }else {
            // 1.接收请求参数
            String phone = param.get("phone");
            // 2.调用manager
            userManager.sendSms(phone);
            return ResponseEntity.ok("允许登陆");
        }
    }

    // 注册登录
    @PostMapping("/user/loginVerification")
    public ResponseEntity regAndLogin(@RequestBody Map<String, String> param) {


            // 1.接收请求参数
            String phone = param.get("phone");
            String verificationCode = param.get("verificationCode");
            // 2.调用manager
            return userManager.regAndLogin(phone, verificationCode);

    }

    // 完善用户基本信息
    @PostMapping("/user/loginReginfo")
    public void saveUserInfo(@RequestBody UserInfo userInfo, @RequestHeader("Authorization") String token) {
        // 调用manager
        userManager.saveUserInfo(userInfo, token);
    }

    // 完善用户头像信息
    @PostMapping({"/user/loginReginfo/head","/users/header"})
    public ResponseEntity saveHeadPhoto(MultipartFile headPhoto,@RequestHeader("Authorization") String token) throws IOException {
        // 调用manager
        return userManager.saveHeadPhoto(headPhoto,token);
    }

    // 查询用户信息
    @GetMapping("/users")
    public ResponseEntity findUserInfoVo(
            @RequestParam(value = "userID",required = false) Long userId
            ,@RequestHeader("Authorization") String token){
        // 1.接收参数并判断
        if (userId!=null) {
            // 2.调用manager
            return userManager.findUserInfoVo(userId);
        }else{
            User user = userManager.findUserByToken(token);
            // 2.调用manager
            return userManager.findUserInfoVo(user.getId());
        }
    }

    // 更新用户基本信息
    @PutMapping("/users")
    public void updateUserInfo(@RequestBody UserInfo userInfo,@RequestHeader("Authorization") String token){
        // 调用manager
        userManager.updateUserInfo(userInfo,token);
    }

}
